package com.shigen.demo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shigen.demo.annotation.RequestLimit;
import com.shigen.demo.bean.Result;
import com.shigen.demo.entity.UserInfo;
import com.shigen.demo.entity.req.UserInfoReq;
import com.shigen.demo.entity.req.UserInfoReq.DelBatch;
import com.shigen.demo.service.IUserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping(value = "userinfo")
@Api(tags = "用户信息接口")
@RequestLimit
public class userInfoController {

  @Autowired
  private IUserInfoService userInfoService;

  @PostMapping(value = "add")
  @ApiOperation(value = "添加用户信息")
  public Object addUser(@Valid @RequestBody UserInfoReq.OpBase opBase) {
    Integer userid = userInfoService.addUserInfo(opBase);
    return userid.equals(-1) ? Result.err("用户添加失败") : Result.ok(userid, "用户添加成功");
  }

  @DeleteMapping(value = "delete")
  @ApiOperation(value = "批量删除用户信息")
  public Object delUser(@RequestBody @Valid DelBatch delBatch) {
    Integer result = userInfoService.delBatch(delBatch.getUserIds());
    return result > 0 ? Result.ok("已成功删除" + result + " 条") : Result.err("用户删除失败");
  }

  @PutMapping(value = "updatestatus/{userid}/{status}")
  @ApiOperation(value = "更新用户信息状态")
  public Object updateUserStatus(@PathVariable("userid") Integer userid, @PathVariable(value = "status") boolean status) {
    Integer result = userInfoService.updateUserInfoStatus(userid, status);
    return result > 0 ? Result.ok("用户状态修改成功") : Result.err("用户修改失败");
  }

  @PutMapping(value = "update")
  @ApiOperation(value = "更新用户信息")
  public Object updateUserInfo(@RequestBody @Valid UserInfoReq.OpBase userInfo) {
    Integer result = userInfoService.updateUserInfo(userInfo);
    return result > 0 ? Result.ok("用户信息修改成功") : Result.err("用户信息修改失败");
  }

  @PostMapping(value = "select")
  @ApiOperation(value = "分页查询用户信息")
  public Result<IPage<UserInfo>> selectUserInfos(@RequestBody @Valid UserInfoReq.QueryItem queryInfo) {
    IPage<UserInfo> allUserInfo = userInfoService.getAllUserInfo(queryInfo);
    return Result.ok(allUserInfo);
  }

  @GetMapping(value = "select/{userid}")
  @ApiOperation(value = "根据userid查询用户信息")
  public Result<UserInfo> selectUserInfoByUserId(@PathVariable("userid") Integer userId) {
    UserInfo userInfo = userInfoService.getUserInfoByUserId(userId);
    return userInfo == null ? Result.err("用户不存在或已删除") : Result.ok(userInfo);
  }

  @PostMapping(value = "updateicon")
  @ApiOperation(value = "更新当前用户头像")
  public Result<String> updateUserIcon(@RequestParam("file") MultipartFile file) {
    userInfoService.updateUserIcon(file);
    return Result.ok("更新成功");
  }

  @GetMapping(value = "getusericon/{userid}")
  @ApiOperation(value = "获得用户头像的存储路径")
  public Result<Object> getUserIcon(@PathVariable("userid") Integer userid) {
    String userIcon = userInfoService.getUserIcon(userid);
    return Result.ok(userIcon);
  }
}
